<template>
    <div class="container">
        <div class="main">
            <div class="pay-title">
                支付总金额 <span class="pay-price">￥ {{payData.price}}</span>
            </div>
            <div class="pay-main">
                <h4>微信支付</h4>
                <el-row type="flex" 
                justify="space-between" 
                align="middle"
                class="pay-qrcode">
                    <div class="qrcode">
                        <!-- 二维码 -->
                        <canvas ref="canvas" id="qrcode-stage"></canvas>
                        <p>请使用微信扫一扫</p>
                        <p>扫描二维码支付</p>
                    </div>
                    <div class="pay-example">
                        <img src="http://157.122.54.189:9093/images/wx-sweep2.jpg" alt="">
                    </div>
                </el-row>
            </div>
        </div>
    </div>
</template>

<script>
// 二维码生成插件
import Qrcode from 'qrcode'
export default {
  data() {
    return {
      payData: {},
      time: null
    }
  },
  mounted() {
    // 页面加载先执行第一次 $store一旦有变化watch会执行
    // setTimeout(() => {
    //   this.loadAir()
    // }, 200)
  },
  watch: {
    '$store.state.user.userinfo.token': {
      // 页面刷新的话token会被清空 token回不来 页面就会报错
      // 所以我们监听store 一旦有了token我们才请求数据
      handler() {
        if (this.$store.state.user.userinfo.token) {
          this.loadAir()
        }
      },
      immediate: true
    }
  },
  methods: {
    // 获取订单数据
    loadAir() {
      this.$axios({
        url: '/airorders/' + this.$route.query.id,
        headers: {
          Authorization: 'Bearer ' + this.$store.state.user.userinfo.token
        }
      }).then(res => {
        console.log(res.data)
        this.payData = res.data
        const code = this.$refs.canvas
        //调用插件 生成二维码
        Qrcode.toCanvas(code, this.payData.payInfo.code_url, {
          width: 200
        })
        this.getPayStatus()
      })
    },
    // 获取订单支付状态
    getPayStatus() {
      this.$axios({
        url: '/airorders/checkpay',
        method: 'post',
        data: {
          id: this.payData.id,
          nonce_str: this.payData.price,
          out_trade_no: this.payData.orderNo
        },
        headers: {
          Authorization: 'Bearer ' + this.$store.state.user.userinfo.token
        }
      }).then(res => {
        console.log(res.data)
        if (res.data.trade_state == 'NOTPAY') {
          this.time = setTimeout(() => {
            this.getPayStatus()
          }, 2000)
        } else if (res.data.trade_state == 'SUCCESS') {
          this.$message({
            message: '支付成功',
            type: 'success'
          })
        }
      })
    }
  },
  // 轮询支付状态 跳转其他页面还是会继续执行
  // 我们要在页面离开前清除定时器
  beforeDestroy() {
    clearTimeout(this.time)
    console.log('清除了定时器')
  }
}
</script>

<style scoped lang="scss">
.container {
  background: #f5f5f5;
  padding: 30px 0;

  .main {
    width: 1000px;
    margin: 0 auto;

    .pay-title {
      text-align: right;
      span {
        font-size: 28px;
        color: orangered;
      }
    }

    .pay-main {
      background: #fff;
      margin-top: 10px;
      border-top: 5px orange solid;
      padding: 30px;

      h4 {
        font-size: 28px;
        font-weight: normal;
        margin-bottom: 10px;
      }

      .pay-qrcode {
        padding: 0 80px;
      }

      .qrcode {
        border: 1px #ddd solid;
        padding: 15px;
        height: fit-content;

        #qrcode-stage {
          width: 200px;
          height: 200px;
          margin-bottom: 10px;
        }

        p {
          line-height: 2;
          text-align: center;
        }
      }
    }
  }
}
</style>